<?php
namespace app\admin\model;

use think\Model;

class Information extends Model{
    /**
     * 获取所有记录
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $order = "";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.username|u.full_name|u.tel'=> ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $data = $this
            ->alias('i')
            ->join('user u','i.user_id=u.id','LEFT')
            ->field('i.*,u.full_name')
            ->where($where)
            ->order($order)
            ->select();
        if($data){
            foreach($data as $key => $value){
                $data[$key] = $value->toArray();
            }
        }

        return $data;
    }

    /**
     * 获取记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order = "";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.username|u.full_name|u.tel'=> ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count = $this
            ->alias('i')
            ->join('user u','i.user_id=u.id','LEFT')
            ->field('i.*,u.full_name')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

    /**
     * 获取记录详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=""){
        $info = $this
            ->alias('i')
            ->join('user u','i.user_id=u.id','LEFT')
            ->field('i.*,u.full_name,u.username,u.tel')
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }

    /**
     * 根据id获取信息详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getInformationById($id){
        $info = $this
            ->alias('i')
            ->join('user u','i.user_id=u.id','LEFT')
            ->where('i.id='.$id)
            ->field('i.*,u.full_name,u.username,u.tel')
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }
}